package com.videoaggre.zcl.ui.fragment.meizuo

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import com.videoaggre.zcl.api.MEIZUO_SERVICE
import com.videoaggre.zcl.bean.EnterdeskClassify
import com.videoaggre.zcl.common.ENTERDESK_CLASSIFY
import com.videoaggre.zcl.common.ENTERDESK_IMAGE
import com.videoaggre.zcl.common.startKtxActivity
import com.videoaggre.zcl.helper.InjectBundle
import com.videoaggre.zcl.parse.impl.MeizuoParse
import com.videoaggre.zcl.ui.activity.wallpaper.meizuo.MeizuoImageDetailActivity
import com.videoaggre.zcl.ui.fragment.enterdesk.EnterdeskImageFragment
import com.videoaggre.zcl.ui.fragment.launchUI
import com.videoaggre.zcl.ui.fragment.put
import com.videoaggre.zcl.ui.viewmodel.EnterdeskViewModel
import com.videoaggre.zcl.utils.Logger

open class MeizuoImageFragment : EnterdeskImageFragment() {
    companion object {
        fun createInstance(enterdeskClassify: EnterdeskClassify): MeizuoImageFragment = MeizuoImageFragment().put(ENTERDESK_CLASSIFY to enterdeskClassify)
    }

    private val mEnterdeskViewModel: EnterdeskViewModel by viewModels(::requireActivity)
    private val mEnterdeskClassify: EnterdeskClassify by InjectBundle()
    private var mResolutionEnterdeskClassify: EnterdeskClassify? = null
    override fun onClick(view: View, position: Int) {
        startKtxActivity<MeizuoImageDetailActivity>(value = ENTERDESK_IMAGE to mImageShowAdapter.getItem(position))
    }

    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        super.initView(rootView, savedInstanceState)
        mEnterdeskViewModel.brandDetilChangeLiveData.observe(this) {
            mResolutionEnterdeskClassify = it
            mRefreshHelper.onRefresh()
        }
    }

    override fun onReload() {
        launchUI(
            {
                MeizuoParse.parseImages(
                    MEIZUO_SERVICE.meizuoMobile(
                        mEnterdeskClassify.path,
                        mResolutionEnterdeskClassify?.path ?: "0",
                        mRefreshHelper.page
                    )
                )
            },
            mRefreshHelper.handApiObserver(error = { mRefreshHelper.doSuccess(mutableListOf()) })
        )
    }
}